package wordit.impl.boardwise;

import java.util.List;

import wordit.ea.Genome;
import wordit.ea.MutationFunction;

public class BoardwiseMutation implements MutationFunction {

	private double mutationProbability;

	public BoardwiseMutation(double mutationProbability) {
		this.mutationProbability = mutationProbability;
	}

	@Override
	public Genome mutateGenom(Genome gen) {
		if (Math.random() >= getMutationProbability())
			return gen;

		// moves a single letter to some vacant location

		final BoardGenome board = (BoardGenome) gen;
		List<BoardLocation> occupiedLocations, vacantLocations;
		BoardLocation from, to;

		occupiedLocations = board.getOccupiedLocations();
		vacantLocations = board.getVacantLocations();
		from = occupiedLocations.get((int) Math.floor(Math.random()
				* occupiedLocations.size()));
		to = vacantLocations.get((int) Math.floor(Math.random()
				* vacantLocations.size()));

		BoardGenome newBoard = new BoardGenome(board);
		newBoard.moveLetter(from, to);

		return newBoard;
	}

	public double getMutationProbability() {
		return mutationProbability;
	}

	public void setMutationProbability(double mutationProbability) {
		this.mutationProbability = mutationProbability;
	}

}
